package com.wupo.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wupo.common.base.BaseResponseBean;
import com.wupo.common.utils.SearchUtil;
import com.wupo.common.vo.ConditionVo;
import com.wupo.common.vo.PageVo;
import com.wupo.common.vo.SearchVo;
import com.wupo.demo.entity.SysDept;
import com.wupo.demo.service.ISysDeptService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门 Controller
 *
 * @author wupo
 * @since 2020-02-23
 */
@Slf4j
@RestController
@RequestMapping("/sys/dept")
@Api(tags = "SysDept", description = "部门CURD")
public class SysDeptController {

    @Autowired
    private ISysDeptService sys_deptService;

    @ApiOperation(value = "list", notes = "列表", httpMethod = "GET")
    @GetMapping
    public BaseResponseBean list() {
        return BaseResponseBean.success(sys_deptService.list());
    }

    @ApiOperation(value = "getSysDept", notes = "根据ID获取对象", httpMethod = "GET")
    @ApiImplicitParam(required = true, name = "id", defaultValue = "1")
    @GetMapping("/get/{id}")
    public BaseResponseBean get(@PathVariable("id") Long id) {
        return BaseResponseBean.success(sys_deptService.getById(id));
    }

    @ApiOperation(value = "Save", notes = "新增", httpMethod = "POST")
    @PostMapping("/save")
    public BaseResponseBean save(@RequestBody SysDept sys_dept) {
        return BaseResponseBean.success(sys_deptService.save(sys_dept));
    }

    @ApiOperation(value = "Update", notes = "更新")
    @PutMapping("/update")
    public BaseResponseBean updateById(@RequestBody SysDept sys_dept) {
        return BaseResponseBean.success(sys_deptService.updateById(sys_dept));
    }

    @ApiOperation(value = "UaveAndUpdate", notes = "新增或更新", httpMethod = "POST")
    @PostMapping
    public BaseResponseBean saveOrUpdate(@RequestBody SysDept sys_dept) {
        return BaseResponseBean.success(sys_deptService.saveOrUpdate(sys_dept));
    }

    @ApiOperation(value = "Remove", notes = "移除", httpMethod = "DELETE")
    @DeleteMapping("/del")
    public BaseResponseBean remove(@RequestBody SysDept sys_dept) {
        return BaseResponseBean.success(sys_deptService.removeById(sys_dept));
    }

    /**
     * 测试
     *
     * @param sysDept
     * @return
     */
    @GetMapping("/test")
    public BaseResponseBean test(SysDept sysDept) {
        sysDept.setDeptId(11l);
        sysDept.setDeptName("test");
        sysDept.setStatus("0");
        sysDept.setDelFlag("0");
        log.info("测试" + sysDept);
        return BaseResponseBean.success(sysDept);
    }

    /**
     * 统计
     *
     * @param conditions
     * @return
     */
    @ApiOperation(value = "Count", notes = "统计", httpMethod = "POST")
    @PostMapping("/count")
    public BaseResponseBean count(@RequestBody List<ConditionVo> conditions) {
        return BaseResponseBean.success(sys_deptService.count(SearchUtil.parseWhereSql(conditions)));
    }

    /**
     * 简单分页demo
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "PageList", notes = "简单分页", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "pageNum", defaultValue = "1"),
            @ApiImplicitParam(required = true, name = "pageSize", defaultValue = "10")
    })
    @PostMapping("/page/simple")
    public BaseResponseBean pageList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Page<SysDept> page = new Page<>(pageNum, pageSize);
        return BaseResponseBean.success(sys_deptService.page(page));
    }

    /**
     * 分页demo
     *
     * @param pageParam
     * @return
     */
    @ApiOperation(value = "PageList", notes = "分页列表", httpMethod = "POST")
    @PostMapping("/page")
    public BaseResponseBean pageList(@RequestBody PageVo pageParam) {
        Integer current = pageParam.getPageNum() == null ? 1 : pageParam.getPageNum();
        Integer size = pageParam.getPageSize() == null ? 10 : pageParam.getPageSize();
        Page<SysDept> page = new Page<>(current, size);
        page.setOrders(pageParam.getOrders());
        return BaseResponseBean.success(sys_deptService.page(page));
    }

    /**
     * 搜索demo
     *
     * @param searchParam
     * @return
     */
    @ApiOperation(value = "SearchPageList", notes = "搜索分页列表", httpMethod = "POST")
    @PostMapping("/search")
    public BaseResponseBean searchList(@RequestBody SearchVo searchParam) {
        Page<SysDept> page = new Page<>(searchParam.getPageNum() == null ? 1 : searchParam.getPageNum(), searchParam.getPageSize() == null ? 10 : searchParam.getPageSize());
        page.setOrders(searchParam.getOrders());
        return BaseResponseBean.success(SearchUtil.parseWhereSql(searchParam.getConditions()));
    }

}
